package com.zw.yzk.kabu.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.model.SubmitGoods;
import com.zw.yzk.kabu.model.SubmitOrder;
import com.zw.yzk.kabu.utils.ResUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhanwei on 2017/3/12.
 */

public class ConfirmGoodsOrderShopAdapter extends RecyclerView.Adapter<ConfirmGoodsOrderShopAdapter.Holder> {

    private Context context;
    private List<SubmitOrder> dataList;

    public ConfirmGoodsOrderShopAdapter(List<SubmitOrder> dataList) {
        this.dataList = new ArrayList<>();
        if (dataList != null) {
            this.dataList.addAll(dataList);
        }
    }

    public void refresh(List<SubmitOrder> dataList) {
        this.dataList.clear();
        if (dataList != null) {
            this.dataList.addAll(dataList);
        }
        notifyDataSetChanged();
    }

    public List<SubmitOrder> getOrderList() {
        return dataList;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null) {
            context = parent.getContext();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.recycler_item_confirm_goods_order_shop_list, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        SubmitOrder order = dataList.get(position);
        holder.shopName.setText(order.getShopName());
        holder.goodsCount.setText(context.getResources().getString(R.string.order_goods_count, order.getShopGoodsCount()));
        holder.goodsPrice.setText(ResUtils.formatCurrency(context, order.getGoodsPrice() + order.getShopFreight()));
        String freight = "(含运费" + ResUtils.formatCurrency(context, order.getShopFreight()) + ")";
        holder.freight.setText(freight);
        holder.setAdapter(context, order.getGoodsList());
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public static class Holder extends RecyclerView.ViewHolder {

        @Bind(R.id.shop_name)
        TextView shopName;
        @Bind(R.id.goods_count)
        TextView goodsCount;
        @Bind(R.id.total_price)
        TextView goodsPrice;
        @Bind(R.id.goods_list)
        RecyclerView goodsList;
        @Bind(R.id.freight)
        TextView freight;

        private ConfirmGoodsOrderGoodsAdapter adapter;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setAdapter(Context context, List<SubmitGoods> list) {
            goodsList.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
            if (adapter == null) {
                adapter = new ConfirmGoodsOrderGoodsAdapter(list);
                goodsList.setAdapter(adapter);
            } else {
                adapter.refresh(list);
            }
        }
    }
}
